package com.ohyes.bigbang.demo.helloworld.controller;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ohyes.bigbang.commons.base.BaseController;
import com.ohyes.bigbang.demo.helloworld.service.IHelloWorldService;
import com.ohyes.bigbang.demo.model.User;

@Controller
@RequestMapping("/hello")
public class HelloWorldController extends BaseController {

	private static final Logger logger = LoggerFactory
			.getLogger(HelloWorldController.class);

	@Autowired
	IHelloWorldService helloWorldService;

	@RequestMapping("/noajax")
	public ModelAndView noajax() {
		String message = "Hello World, Spring 3.0!";
		System.out.println(message);
		User user = new User();
		user.setId(1);
		user.setName("神马");
		user.setAge(124);
		return new ModelAndView("/test-no-ajax/noajax", "user", user);
	}

	@RequestMapping("/noajaxnoparam")
	public String nanp() {
		return "/test-no-ajax/noajax";
	}
	
	@RequestMapping("/index")
	public String index() {
		return "/framework/index/index";
	}

	@RequestMapping("/ajax")
	public String ajax() {

		return "/test-ajax/ajax";
	}

	@RequestMapping("/demo")
	public String demo() {
		return "/demo/demo";
	}

	@RequestMapping("/testParams")
	public @ResponseBody
	HashMap<String, Object> testParams(User user) {
		HashMap<String, Object> result = new HashMap<String, Object>();
		result.put("sucess", true);
		result.put("kkk", user);
		result.put("message", "悠嘻花姑娘~");
		return result;
	}

	/**
	 * CRUD
	 */
	@RequestMapping("/query")
	public String query() {
		return "/demo/crud/query";
	}

	@RequestMapping("/getList")
	public @ResponseBody
	HashMap<String, Object> getList(User user) {
		HashMap<String, Object> result = new HashMap<String, Object>();
		int count = helloWorldService.getCount(user);
		if (count != 0) {
			List<HashMap<String, Object>> list = helloWorldService
					.getList(user);
			result.put("totalCount", count);
			result.put("items", list);
		}
		return result;
	}

	@RequestMapping("testIO")
	public void testIO() {
		File f = new File("C:/11071.txt");
		for (int i = 0; i < 500; i++) {
			System.out.println("readFile===" + i);
			this.readFile(f);
		}

	}

	public void readFile(File f) {
		BufferedReader reader = null;
		try {

			reader = new BufferedReader(new FileReader(f));
			String tempString = null;
			int line = 1;
			// 一次读入一行，直到读入null为文件结束
			while ((tempString = reader.readLine()) != null) {
				// 显示行号
				line++;
			}
			// TODO
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e1) {
				}
			}
		}
	}

	@RequestMapping("/save")
	public @ResponseBody
	HashMap<String, Object> save(User user, String operType) {
		HashMap<String, Object> result = new HashMap<String, Object>();
		try {
			if ("add".equals(operType)) {
				helloWorldService.add(user);
			} else {
				helloWorldService.update(user);
			}

			result.put("success", true);
		} catch (Exception e) {
			// TODO: handle exception
			result.put("success", false);
			result.put("msg", e.getMessage());
		}

		return result;
	}

	@RequestMapping("/delete")
	public @ResponseBody
	HashMap<String, Object> delete(User user) {
		HashMap<String, Object> result = new HashMap<String, Object>();
		try {
			helloWorldService.delete(user);

			result.put("success", true);
		} catch (Exception e) {
			// TODO: handle exception
			result.put("success", false);
			result.put("msg", e.getMessage());
		}

		return result;
	}
}